package Workflow.States.Impl;

import Model.Core.IElement;
import Model.Core.Tip.TipMoment;
import Model.Core.Tip.TipType;
import Model.Draw.Events.DrawCompleteListener;
import Model.Draw.Events.ShowTipEvent;
import Model.Draw.Events.ShowTipListener;
import Workflow.States.IState;
import Workflow.Util.IncompatibleStateException;
import java.util.ArrayList;
import java.util.List;

/**
 * Projeto de TCC: Editor de Diagramas de Comunicação em UML.
 * @author jose.luz
 */
public class StartUpState implements IState {

    private IElement currentElement;
    private List<DrawCompleteListener> drawListeners;
    private List<ShowTipListener> tipListeners;

    public StartUpState(List<DrawCompleteListener> listeners, List<ShowTipListener> tipsListener) {
        this.drawListeners = listeners;
        this.tipListeners = tipsListener;
    }

    @Override
    public IState nextState() {
        return new BeginState(currentElement, drawListeners, tipListeners);
    }

    @Override
    public void setItem(Object element) throws IncompatibleStateException {
        throw new IncompatibleStateException("Erro no armazenamento das informações. Tente novamente");
    }

    @Override
    public void registerDrawCompleteListener(DrawCompleteListener listener) {
        drawListeners.add(listener);
    }

    @Override
    public void registerShowTipsListener(ShowTipListener listener) {
        tipListeners.add(listener);
    }

    @Override
    public IElement getCurrentElement() {
        return currentElement;
    }

    @Override
    public void setCurrentElement(IElement element) throws IncompatibleStateException {
        this.currentElement = element;
        notifyTipListeners();
    }

    @Override
    public List<Object> getItem() {
        return new ArrayList<Object>();
    }

    private void notifyTipListeners() {
        for (ShowTipListener listener : tipListeners) {
            listener.ShowTipHandler(new ShowTipEvent(currentElement.findTipBy(TipType.Hint, TipMoment.Source)));
        }
    }
}